package com.bw.documents.local

import android.annotation.SuppressLint
import android.database.Cursor
import android.provider.MediaStore
import android.util.Log
import com.bw.baseapplication.BaseApplication
import com.bw.common.mvi.BaseRespository
import com.bw.documents.local.entity.LocalDocumentsEntity
import com.bw.documents.local.entity.LocalDocumentsType
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import java.io.File

class LocalRepo : BaseRespository() {

    //查询本地文件
    @SuppressLint("Range")
    fun scanLocalDocuments():Flow<List<LocalDocumentsEntity>>{
        return flow {
            val datas = ArrayList<LocalDocumentsEntity>()
            val file = File(BaseApplication.baseApplication.getExternalFilesDir(null)!!.absolutePath)
            Log.e("ZXY",""+file.absolutePath)
            val fileDatas = file.listFiles()
            fileDatas.forEach {
                if (it.isFile){
                    var type : LocalDocumentsType?=null
                    var str = it.path.substring(it.path.lastIndexOf("."))
                    Log.e("ZXY","str:"+str)
                    if (str == ".pdf")
                        type = LocalDocumentsType.DOCUMENTS
                    else if (str == ".txt")
                        type = LocalDocumentsType.DOCUMENTS
                    else if (str == ".mp3")
                        type = LocalDocumentsType.MUSIC
                    else if (str == ".mp4")
                        type = LocalDocumentsType.VIDEO
                    else type = LocalDocumentsType.IMG
                    val entity  = LocalDocumentsEntity(it.name,it.path, type!!,false)
                    datas.add(entity)
                }
            }
            emit(datas)
        }
    }

}